package com.geek.bams.dao;

import com.geek.bams.entity.Account;

import java.util.Arrays;

public class ArrayDao {
    //存储Account账户信息的容器
    private Account[] accs = new Account[5];
    //统计Account账户数量
    private int index = 0;

    public boolean insert(Account acc){
        if(index>=accs.length){
            //扩容操作
            accs = Arrays.copyOf(accs , accs.length*3/2+1);
        }
        //添加元素
        accs[index++] = acc;
        return true;
    }

    //根据ID和密码查询账户
    public Account selectOne(Long id , String password){
        for (int i = 0; i < index ; i++) {
            Account acc = accs[i];
            if(acc!=null && acc.getId().equals(id) && acc.getPassword().equals(password)){
                return acc;
            }
        }
        return null;
    }

    //根据ID查询账户
    public Account selectOne(Long id){
        for (int i = 0; i < index ; i++) {
            Account acc = accs[i];
            if(acc!=null && acc.getId().equals(id)){
                return acc;
            }
        }
        return null;
    }

    //查询所有账户
    public Account[] selectAll(){
        return accs;
    }
}
